<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" :clearable="true" @clear="search">
            <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="openActDialog">新建用户</el-button>
        </el-col>
      </el-row>
      <!-- 用户列表区域 -->
      <el-table :data="userList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="username" label="姓名" width="180"></el-table-column>
        <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
        <el-table-column prop="mobile" label="电话" width="180"></el-table-column>
        <el-table-column prop="role_name" label="角色" width="180"></el-table-column>
        <el-table-column label="状态" width="180">
          <template v-slot:default="scope">
            <el-switch v-model="scope.row.mg_status" active-color="#13ce66" inactive-color="#ff4949" @change="userStateChange(scope.row.mg_status)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <!-- {{scope.row}} -->
            <el-button type="danger" size="mini" icon="el-icon-edit"></el-button>
            <el-button type="warning" size="mini" icon="el-icon-delete"></el-button>
            <el-tooltip class="item" effect="dark" content="分配角色" placement="top" :enterable="false">
              <el-button type="primary" size="mini" icon="el-icon-setting"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 3, 4]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <el-dialog title="添加用户" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form ref="addFormRef" :model="addForm" label-width="70px" :rules="addFormRules">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { userGetApi, addUserPostApi } from '../../request/api'
export default {
  data () {
    // 自定义校验规则
    const checkEmail = (rule, value, callback) => {
      const regEmail = /^[\w\d]+@[\w\d]+\.+[\w]+$/
      if (regEmail.test(value)) {
        callback()
      } else {
        callback(new TypeError('输入的邮箱格式错误！'))
      }
    }
    const checkPhone = (rule, value, callback) => {
      const regPhone = /\d{11}/
      if (regPhone.test(value)) {
        callback()
      } else {
        callback(new TypeError('输入的手机格式错误！'))
      }
    }
    return {
      provideData: 'nnn',
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 3
      },
      total: 0,
      userList: [],
      dialogVisible: false,
      addForm: {
        username: '',
        password: '',
        email: '',
        phone: ''
      },
      addFormRules: {
        username: [{
          required: true,
          message: '请输入用户名！',
          trigger: 'blur'
        }, {
          min: 3,
          max: 10,
          message: '字符长度在3-10之间！',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '请输入密码！',
          trigger: 'blur'
        }, {
          min: 3,
          max: 10,
          message: '字符长度在3-10之间！',
          trigger: 'blur'
        }],
        email: [{
          required: true,
          message: '请输入邮箱！',
          trigger: 'blur'
        }, {
          min: 3,
          max: 10,
          message: '字符长度在3-10之间！',
          trigger: 'blur'
        }, {
          validator: checkEmail,
          trigger: 'blur'
        }],
        phone: [{
          required: true,
          message: '请输入手机！',
          trigger: 'blur'
        }, {
          min: 11,
          max: 11,
          message: '字符长度为11位！',
          trigger: 'blur'
        }, {
          validator: checkPhone,
          trigger: 'blur'
        }]
      }

    }
  },
  created () {
    this.getUserList()
  },
  methods: {
    getUserList () {
      userGetApi({ params: this.queryInfo }).then(res => {
        const { data } = res
        if (data.meta.status !== 200) {
          this.$message.error = '获取人员失败！'
          return
        }
        this.userList = data.data.users
        this.total = data.data.total
        this.queryInfo.pagenum = data.data.pagenum
        // console.log(this.userList)
      })
    },
    // 监听pagesize
    handleSizeChange (nowSize) {
      // console.log(nowSize)
      this.queryInfo.pagesize = nowSize
      this.getUserList()
    },
    // 监听 页码值
    handleCurrentChange (nowPage) {
      // console.log(nowPage)
      this.queryInfo.pagenum = nowPage
      this.getUserList()
    },
    userStateChange (nowState) {
      console.log(nowState)
      // 发送put请求来进行修改数据
    },
    search () {
      // 重新发送get请求来进行查询
      this.getUserList()
    },
    openActDialog () {
      this.dialogVisible = true
    },
    handleClose () {
      // 关闭前做点什么
      this.$refs.addFormRef.resetFields()
      this.dialogVisible = false
    },
    addUser () {
      this.$refs.addFormRef.validate(async vaild => {
        if (!vaild) return
        const loadingInstance = this.$loading.service({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        const { data } = await addUserPostApi(this.addForm)
        if (data.meta.status !== 201) {
          this.$message.error('用户添加失败！')
        } else {
          this.$message.success('用户添加成功！')
          this.dialogVisible = false
        }
        loadingInstance.close()
      })
    }
  },
  watch: {
    'queryInfo.query': {
      handler (nVal, oVal) {
        this.getUserList()
      }
    }
  }
}
</script>
<style lang='less' scoped>
</style>
